/*
 * DialogGepasseerdeWebinar.java
 *
 * Created on 25-nov-2011, 15:56:57
 */
package gui;

import domein.Webinar;
import domein.WebinarController;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.*;
import javax.swing.DefaultListModel;
import persistentie.PersistentieFactory;

/**
 *
 * @author Karolien Sabbe
 */
public class DialogGepasseerdeWebinar extends javax.swing.JDialog {

    int webID;
    WebinarController wc;
    Webinar w;
    DefaultListModel lijstVraagEnAntwoord;

    /** Creates new form DialogGepasseerdeWebinar */
    public DialogGepasseerdeWebinar(java.awt.Frame parent, boolean modal, Webinar web, int gid) {
        super(parent, modal);
        w = web;
        wc = WebinarController.getInstance();
        webID = web.getWebinarID();
        lijstVraagEnAntwoord = new DefaultListModel();
        for (String s : PersistentieFactory.getWebinarMapper().geefVraagEnAntwoord(webID)) {
            lijstVraagEnAntwoord.addElement(s);
        }
        initComponents();

        //positie panel:
        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension screenSize = tk.getScreenSize();
        int screenWidth = screenSize.width;

        setLocation((screenWidth - 824) / 2, 100);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        webinarList = new javax.swing.JList();
        downloadButton = new javax.swing.JButton();
        sluitButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        webinarList.setModel(lijstVraagEnAntwoord);
        webinarList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(webinarList);

        downloadButton.setText("Downloaden");
        downloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downloadButtonActionPerformed(evt);
            }
        });

        sluitButton.setText("Sluiten");
        sluitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sluitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(159, Short.MAX_VALUE)
                .addComponent(downloadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sluitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sluitButton)
                    .addComponent(downloadButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed
    FileOutputStream fos;
    DataOutputStream dos;

    try {
        File file = new File("C:\\Webinar_" + w.getOnderwerp() + ".txt");
        fos = new FileOutputStream(file);
        dos = new DataOutputStream(fos);
        for (String s : PersistentieFactory.getWebinarMapper().geefVraagEnAntwoord(webID)) {
            dos.writeChars(s);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}//GEN-LAST:event_downloadButtonActionPerformed

private void sluitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sluitButtonActionPerformed
    dispose();
}//GEN-LAST:event_sluitButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton downloadButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton sluitButton;
    private javax.swing.JList webinarList;
    // End of variables declaration//GEN-END:variables
}
